﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBTool
{
    using DB_TABLE_NAME = System.String;

    public partial class MonsterSkillConditionTableMeta : TableMeta
    {
        public MonsterSkillConditionTableMeta()
        {
            _createTableName.Add( "monster_skill_condition" );

            _rowGropList.Add( new KeyValuePair< DB_TABLE_NAME, string[] >( "monster_skill_condition", Group1 ) );
            _rowGropList.Add( new KeyValuePair< DB_TABLE_NAME, string[] >( "monster_skill_condition", Group2 ) );
            _rowGropList.Add( new KeyValuePair< DB_TABLE_NAME, string[] >( "monster_skill_condition", Group3 ) );
            _rowGropList.Add( new KeyValuePair< DB_TABLE_NAME, string[] >( "monster_skill_condition", Group4 ) );
            _rowGropList.Add( new KeyValuePair< DB_TABLE_NAME, string[] >( "monster_skill_condition", Group5 ) );
        }

        protected override void ExtraProcessByTable( string exportPath )
        {
            MonsterSkillConditionTable_RemoveConditionIsZeroRow( exportPath );
        }

        private void MonsterSkillConditionTable_RemoveConditionIsZeroRow( string exportPath )
        {
            string monsterInfoParsedTablePath = exportPath + "/monster_skill_condition" + Config._exportFileExt;
           
            CSVReader monsterInfoTable = new CSVReader(monsterInfoParsedTablePath);

            List<int> removeRow = new List<int>();

            for (int rowCount = 1; rowCount < monsterInfoTable.GetRowCount(); ++rowCount)
            {
                if ("0" != monsterInfoTable.Find(rowCount, "condition_type"))
                    continue;

                removeRow.Add(rowCount);
            }

            File.Delete(monsterInfoParsedTablePath);

            // Sync Delete File
            while (true == File.Exists(monsterInfoParsedTablePath)) { }

            StreamWriter fileWriter = new StreamWriter(monsterInfoParsedTablePath, false, Encoding.UTF8);

            IList<string> rows = monsterInfoTable.GetRows();
            for (int rowCount = 0; rowCount < rows.Count; ++rowCount)
            {
                if (true == removeRow.Contains(rowCount))
                    continue;

                fileWriter.WriteLine(rows[rowCount]);
            }

            fileWriter.Close();
        }
    }
}
